package com.example.recyclerview0318;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.nio.charset.MalformedInputException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private  void initView(){
        SwipeRecyclerView contactList = findViewById(R.id.recycler_view);
        SmartRefreshLayout refreshLayout = findViewById(R.id.smart_list);
        contactList.setLayoutManager(new LinearLayoutManager(this));

        String[] strings = new String[]{"马化腾","马云","王健林","赵韩杰","汪聪","钱回朝","迪丽热巴","王思聪"};
        int[] ints = new int[]{R.drawable.mahuateng,R.drawable.mayun,R.drawable.wang,R.drawable.zhaohanjie,
                               R.drawable.wangcong,R.drawable.qianhuichao,R.drawable.dilireba,R.drawable.wangsic};
        String[] strings1 = new String[]{"小罗，这个项目没你不行吖","20亿够吗","先搞他几十个小目标","你这大哥我认定了",
                                        "你以后就是我亲哥了","你比我亲哥还亲","[视频通话]","出来玩"};
        String[] strings2 = new String[]{"19:30","19:20","19:10","19:00","18:50","18:20","18:10","18:05"};
        String[] strings3 = new String[]{"2","1","3","5","2","3","5","1","2"};



        String[] string4 = new String[]{"姜文"};
        int[] ints1 = new int[]{R.drawable.jiangwen};
        String[] strings5 = new String[]{"下部戏你必须来男1号"};
        String[] strings6 = new String[]{"18:03"};
        String[] strings7 = new String[]{"2"};




        List<bean> data = new ArrayList<>();
        List<bean> newdata = new ArrayList<>();



        for (int i = 0; i < strings.length; i++) {
            bean b = new bean();
            b.setName(strings[i]);
            b.setImg(ints[i]);
            b.setTalk(strings1[i]);
            b.setTime(strings2[i]);
            b.setNum(strings3[i]);
            data.add(b);

        }
        for (int j = 0; j < string4.length; j++) {
            bean newb = new bean();
            newb.setName(string4[j]);
            newb.setImg(ints1[j]);
            newb.setTalk(strings5[j]);
            newb.setTime(strings6[j]);
            newb.setNum(strings7[j]);
            newdata.add(newb);
        }

      MyAdapter adapter = new MyAdapter(this);
        adapter.setData(data);


        SwipeMenuCreator menuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteitem = new SwipeMenuItem(MainActivity.this);
                SwipeMenuItem deleteitem1 = new SwipeMenuItem(MainActivity.this);
                SwipeMenuItem deleteitem2 = new SwipeMenuItem(MainActivity.this);

                deleteitem2.setBackground(android.R.color.holo_blue_dark);
                deleteitem2.setText("标为未读");
                deleteitem2.setTextColor(Color.WHITE);
                deleteitem2.setTextSize(14);
                deleteitem2.setHeight(RecyclerView.LayoutParams.MATCH_PARENT);
                deleteitem2.setWidth(340);
                rightMenu.addMenuItem(deleteitem2);

                deleteitem1.setBackground(android.R.color.holo_orange_dark);
                deleteitem1.setText("不显示");
                deleteitem1.setTextColor(Color.WHITE);
                deleteitem1.setTextSize(14);
                deleteitem1.setHeight(RecyclerView.LayoutParams.MATCH_PARENT);
                deleteitem1.setWidth(300);
                rightMenu.addMenuItem(deleteitem1);

                //设置菜单项背景颜色
                deleteitem.setBackground(android.R.color.holo_red_light);
                //设置侧滑菜单文字
                deleteitem.setText("删除");
               //设置侧滑菜单文字颜色
                deleteitem.setTextColor(Color.WHITE);
                //设置侧滑菜单文字大小
                deleteitem.setTextSize(14);
                //设置侧滑菜单文字高度
                //菜单高度需要设置与条目高度相同，所以通常使用RecyclerView.LayoutParams.MATCH_PARENT
                deleteitem.setHeight(RecyclerView.LayoutParams.MATCH_PARENT);
                //设置侧滑菜单文字宽度
                deleteitem.setWidth(260);
                //将菜单项添加到右侧菜单中
               rightMenu.addMenuItem(deleteitem);



            }
        };
        //设置菜单时不可在setAdapter后之后调用
        contactList.setSwipeMenuCreator(menuCreator);
        contactList.setOnItemMenuClickListener(new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
                //判断点击是否是右侧菜单
                if (menuBridge.getDirection()==SwipeRecyclerView.RIGHT_DIRECTION){
                    //根据SwipeMenuCreator创建时添加的顺序判断具体的某个菜单
                    //先添加的序号为0，之后一次类推，类似数组脚标
                    if (menuBridge.getPosition()==0){
                        //点击时收起菜单
                        menuBridge.closeMenu();
                    }
                }
            }
        });


        //延迟自动刷新，单位ms
        //refreshLayout.autoRefresh(200);
        //延迟自动加载，单位ms
      //  refreshLayout.autoLoadMore(200);
        //如果设置了刷新/加载的监听，则默认的建听收起延时会消失
        //需要手动结束刷新/加载

        //单独监听刷新事件
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull  RefreshLayout refreshLayout) {

                //数据和适配器之间以一一匹配，即一套list数据对一个Adapter适配器
                //无论如何刷新，数据list对象并没有发生变化，仅修改数据
                data.clear();
                data.addAll(newdata);
                //无脑刷，刷新全部数据，整个Recycleview重绘
                //当数据刷新完成后，必须通知适配器，否则适配器不会重新绘制复合控件
                adapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(300,true,true);
            }
        });
        //单独监听加载事件
        //移动端分页使用的是增量加载,新数据会累加在老数据之上
        //移动端的分页策略：累加法，将新的数据累加在原有的老数据上
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull  RefreshLayout refreshLayout) {
                //累加新数据
                data.addAll(newdata);
                adapter.notifyDataSetChanged();
                refreshLayout.finishLoadMore(300,true,true);
            }
        });
        contactList.setAdapter(adapter);

        //同时监听加载和刷新
        //单独监听和同时监听不能同时使用
        /*refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(300,true,false);

            }

            @Override
            public void onRefresh(@NonNull  RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(300,false,false);

            }
        });*/

    }
}